package cn.yunhe.servlet;

import cn.yunhe.pojo.UserDemo;
import cn.yunhe.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserRegistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        int flag = Integer.parseInt(request.getParameter("userflag"));
         UserDemo userdemo = new UserDemo(name,pwd,flag);
        UserService  userService = new UserService();
        int result =  userService.registUser(userdemo);
        if(result>0){
            response.sendRedirect("login.jsp");
        }else{
            request.setAttribute("errorregist","注册失败");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
        }
    }
}
